const RegistrationService = require('../services/registration.service')
const LogService = require('../services/log.service')
const PlaceService = require('../services/place.service')
const MessageService = require('../services/message.service')
const FileService = require('../services/file.service')
const logger = require('../utils/logger')
const path = require('path')
const { format } = require('../utils/index')
const fileService = new FileService()
const messageService = new MessageService()
const placeService = new PlaceService()
const logService = new LogService()
const registrationService = new RegistrationService()

class RegistrationController {
  async findAll (ctx, next) {
    const query = ctx.request.query
    if (query.report_state) {
      const logs = await logService.findAll({
        type: query.report_state,
        pageSize: 999999,
        operateDate: {
          $gte: new Date(format(new Date() - 86400000, 'yyyy-MM-dd 00:00:00'))
        }
      })
      query.place = {
        $in: logs.data.map(i => i.ref?._id)
      }
    }
    if (ctx.state.user.role === 'reporter') {
      query.submitter = ctx.state.user._id
    } else if (ctx.state.user.role === 'auditor') {
      query.reg_xzxq = ctx.state.user.area
    }
    const data = await registrationService.findAll(query)
    ctx.response.body = data
  }
  async findOne (ctx, next) {
    const id = ctx.request.params.id
    const data = (await registrationService.findOne(id))[0]
    let reg_syt = []
    if (data.reg_syt && data.reg_syt.length) {
      reg_syt = (await fileService.findAll({ _id: { $in: data.reg_syt } }))
    }
    ctx.response.body = {
      ...data._doc,
      syt: reg_syt.map(i => i.path.replace(path.join(__dirname, '../../uploads'), ''))
    }
  }
  async updateOne (ctx, next) {
    const id = ctx.request.params.id
    const body = ctx.request.body
    if (body.reg_status === 'resolve') {
      delete body.reg_status // 不能手动更新注册状态
    }
    const data = await registrationService.updateOne(id, body)
    ctx.response.body = data
    const ret = await placeService.updateOne(data.place, {
      status: 'filing',
      registration: data._id
    })
    logger.info('place refiling  ', ret._doc)
    logService.addOne({
      type: 'resubmit_registration',
      ref: data._id,
      operator: ctx.state.user._id
    })
  }
  async resolve (ctx) {
    const id = ctx.request.params.id
    const data = await registrationService.resolve(id, ctx.request.body)
    ctx.response.body = data
    logService.addOne({
      type: 'resolve_registration',
      ref: data._id,
      operator: ctx.state.user._id
    })
    messageService.addOne({
      type: 'registration_resolve',
      ref: data._id,
      note: '备案申请审核通过，点击查看详情'
    })
    const ret = await placeService.updateOne(data.place, {
      status: 'filed',
      registration: data._id
    })
    logger.info('place filed  ', ret._doc)
  }
  async reject (ctx) {
    const id = ctx.request.params.id
    const body = ctx.request.body
    const data = await registrationService.reject(id, body)
    ctx.response.body = data
    logService.addOne({
      type: 'reject_registration',
      ref: data._id,
      operator: ctx.state.user._id
    })
    messageService.addOne({
      type: 'registration_reject',
      ref: data._id,
      note: '备案申请审核未通过，点击查看详情'
    })
    const ret = await placeService.updateOne(data.place, {
      status: 'notFiled',
      registration: data._id
    })
    logger.info('place filed  ', ret._doc)
  }
  async addOne (ctx) {
    const body = ctx.request.body
    body.submitter = ctx.state.user._id
    body.reg_status = 'pending'
    const data = await registrationService.addOne(body)
    ctx.response.body = data
    const ret = await placeService.updateOne(data.place, {
      status: 'filing',
      registration: data._id
    })
    logger.info('place filing  ', ret._doc)
    logService.addOne({
      type: 'submit_registration',
      ref: data._id,
      operator: ctx.state.user._id
    })
  }
  async deleteOne (ctx, next) {
    const id = ctx.request.params.id
    const data = await registrationService.deleteOne(id)
    ctx.response.body = data
  }
}

module.exports = RegistrationController